﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
using BLL;
using System.Xml;
using System.Reflection;
using Model;
using System.Linq;

namespace Sales
{
    public partial class PosSet : Form
    {
        private static PosSet posSet = null;
        public Common2 common = new Common2();
        public List<PosFieldDetail> pLs = null;
        public static string config_dir = Application.StartupPath + "/config";

        public static string sysInfo = config_dir + "/PosConfig.xml";
        public static string posFieldDetail = config_dir + "/PosFieldDetail.xml";
        public PosSet()
        {
            InitializeComponent();
            XmlModel entity = common.GetPosConfig(config_dir, sysInfo);//获取相关配置属性 
            ReadXml(entity);
            BindData();
            pLs = common.GetPosConfig_1(config_dir, posFieldDetail);//初始化 
            this.dataGridView2.DataSource = pLs;
        }

        private void BindData()
        {
            DataTable dt = new DataTable();

            dt.Columns.Add("Text");
            dt.Columns.Add("Value");

            DataRow dr = dt.NewRow();
            dr["Text"] = "商品编号";
            dr["Value"] = "spbh";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr["Text"] = "条码号";
            dr["Value"] = "tmh";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["Text"] = "商品名称";
            dr["Value"] = "spmc";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr["Text"] = "规格";
            dr["Value"] = "gg";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr["Text"] = "单位";
            dr["Value"] = "dw";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr["Text"] = "数量";
            dr["Value"] = "sl";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr["Text"] = "零售价";
            dr["Value"] = "lsj";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr["Text"] = "批发价";
            dr["Value"] = "pfj";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr["Text"] = "实收金额";
            dr["Value"] = "ssje";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr["Text"] = "折扣额";
            dr["Value"] = "zke";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr["Text"] = "折扣率";
            dr["Value"] = "zkl";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr["Text"] = "折扣价";
            dr["Value"] = "zhj";
            dt.Rows.Add(dr);


            dr = dt.NewRow();
            dr["Text"] = "供应商名称";
            dr["Value"] = "gysmc";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["Text"] = "进价";
            dr["Value"] = "jj";
            dt.Rows.Add(dr);


            dr = dt.NewRow();
            dr["Text"] = "---请选择---";
            dr["Value"] = "";

            dt.Rows.InsertAt(dr, 0);


            this.cCombox2.DataSource = dt;
            this.cCombox2.DisplayMember = "Text";
            this.cCombox2.ValueMember = "Value";



        }

        private void PosSet_Load(object sender, EventArgs e)
        {

        }

        private void cTabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            PrintShowDialog form = null;
            switch (this.cTabControl1.SelectedIndex)
            {
                case 0:

                    break;
                case 1:
                    //WinForm嵌入panel
                    form = new PrintShowDialog();
                    form.FormBorderStyle = FormBorderStyle.None;
                    form.TopLevel = false;
                    this.panel1.Controls.Add(form);
                    form.Show();


                    break;
            }
        }

        private void gbtnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void gbtnSave_Click(object sender, EventArgs e)
        {
            XmlModel model = new XmlModel();
            //页面设置
            model.PageHeight = this.ctxtPageHeight.Text;
            model.PageWidth = this.ctxtPageWidth.Text;
            //表头
            model.Title = this.ctxtTitle.Text.Trim();
            model.PrintName = GetCheckValue(this.ckMd);
            model.PrintTime = GetCheckValue(this.ckPrintTime);
            model.Site = GetCheckCom(this.cCombox1);
            model.Addinfo1 = this.cTxtAddInfo1.Text;
            model.Addinfo2 = this.cTxtAddInfo2.Text;

            ////表体
            //model.ID = GetCheckValue(this.ckXh);
            //model.Spbh = GetCheckValue(this.ckspbh);
            //model.Spmc = GetCheckValue(this.ckspmc);
            //model.Sl = GetCheckValue(this.ckSl);
            //////////////新增加的字段dw，gg，zk，zhj，pfj

            //model.Dw = GetCheckValue(this.ckDw);
            //model.Gg = GetCheckValue(this.ckGg);
            //model.Zk = GetCheckValue(this.ckZk);
            //model.Zhj = GetCheckValue(this.ckZhj);
            //model.Pfj = GetCheckValue(this.ckPfj);
            //////////////

            //model.Dj = GetCheckValue(this.ckDj);
            //model.Xj = GetCheckValue(this.ckXj);

            //model.IDWidth = this.ctxtID.Text;
            //model.SpbhWidth = this.ctxtSpbh.Text;
            //model.SpmcWidth = this.ctxtSpmc.Text;
            //model.SlWidth = this.ctxtSl.Text;

            //////////////新增加的字段dw，gg，zk，zhj，pfj
            //model.DwWidth = this.ctxtDw.Text;
            //model.GgWidth = this.ctxtGg.Text;
            //model.ZkWidth = this.ctxtZk.Text;
            //model.ZhjWidth = this.ctxtZhj.Text;
            //model.PfjWidth = this.ctxtPfj.Text;
            //////////////

            //model.DjWidth = this.ctxtDj.Text;
            //model.XjWidth = this.ctxtXj.Text;

            ////表尾
            //model.ctxtYw1 = this.ctxtYw1.Text.Trim();
            //model.key1 = GetCheckCom(this.ccomyw1);
            //model.ctxtYw2 = this.ctxtYw2.Text.Trim();
            //model.key2 = GetCheckCom(this.ccomyw2);
            //model.ctxtYw3 = this.ctxtYw3.Text.Trim();
            //model.key3 = GetCheckCom(this.ccomyw3);
            //model.ctxtYw4 = this.ctxtYw4.Text.Trim();
            //model.key4 = GetCheckCom(this.ccomyw4);
            //model.ctxtYw5 = this.ctxtYw5.Text.Trim();
            //model.key5 = GetCheckCom(this.ccomyw5);
            //model.ctxtYw6 = this.ctxtYw6.Text.Trim();
            //model.key6 = GetCheckCom(this.ccomyw6);
            common.SetPosConfig(model, sysInfo);//获取相关配置属性 
            MessageBox.Show("成功");
        }


        private string GetCheckCom(bxyztSkin.CControls.CCombox combox)
        {
            int index = 0;
            if (combox.SelectedText == "右")
            {
                index = 2;
            }
            else if (combox.SelectedText == "中")
            {
                index = 1;
            }
            return index.ToString();
        }

        private void SetCheckCom(bxyztSkin.CControls.CCombox combox, string value)
        {
            if (value == "0")
            {
                combox.SelectedIndex = 2;
            }
            else if (value == "1")
            {
                combox.SelectedIndex = 1;
            }
            else
            {
                combox.SelectedIndex = 0;
            }
        }
        private string GetCheckValue(CheckBox cb)
        {
            string value = "0";

            if (cb.Checked)
            {
                value = "1";
            }
            return value;
        }

        private void SetCheckValue(CheckBox cb, string value)
        {
            if (value == "1")
            {
                cb.Checked = true;
            }
            else
                cb.Checked = false;
        }



        private void PosSet_FormClosed(object sender, FormClosedEventArgs e)
        {
            posSet = null;
            this.Close();
        }


        public void ReadXml(XmlModel model)
        {

            //if (model != null)
            //{

            //    //表头
            //    this.ctxtTitle.Text = model.Title;

            //    this.ctxtPageHeight.Text = model.PageHeight;
            //    this.ctxtPageWidth.Text = model.PageWidth;
            //    SetCheckValue(this.ckMd, model.PrintName);

            //    SetCheckValue(this.ckPrintTime, model.PrintTime);

            //    SetCheckCom(this.cCombox1, model.Site);
            //    this.cTxtAddInfo1.Text = model.Addinfo1;
            //    this.cTxtAddInfo2.Text = model.Addinfo2;

            //    //表体

            //    SetCheckValue(this.ckXh, model.ID);
            //    this.ctxtID.Text = model.IDWidth;
            //    SetCheckValue(this.ckspbh, model.Spbh);
            //    this.ctxtSpbh.Text = model.SpbhWidth;
            //    SetCheckValue(this.ckspmc, model.Spmc);
            //    this.ctxtSpmc.Text = model.SpmcWidth;
            //    SetCheckValue(this.ckSl, model.Sl);
            //    this.ctxtSl.Text = model.SlWidth;
            //    //////////////新增加 字段 dw，gg，zk，zhj，pfj

            //    SetCheckValue(this.ckDw, model.Dw);
            //    this.ctxtDw.Text = model.DwWidth;

            //    SetCheckValue(this.ckGg, model.Gg);
            //    this.ctxtGg.Text = model.GgWidth;

            //    SetCheckValue(this.ckZk, model.Zk);
            //    this.ctxtZk.Text = model.ZkWidth;

            //    SetCheckValue(this.ckZhj, model.Zhj);
            //    this.ctxtZhj.Text = model.ZhjWidth;

            //    SetCheckValue(this.ckPfj, model.Pfj);
            //    this.ctxtPfj.Text = model.PfjWidth;

            //    /////////////
            //    SetCheckValue(this.ckDj, model.Dj);
            //    this.ctxtDj.Text = model.DjWidth;
            //    SetCheckValue(this.ckXj, model.Xj);
            //    this.ctxtXj.Text = model.XjWidth;
            //    //表尾
            //    this.ctxtYw1.Text = model.ctxtYw1;


            //    SetCheckCom(this.ccomyw1, model.key1);

            //    this.ctxtYw2.Text = model.ctxtYw2;

            //    SetCheckCom(this.ccomyw2, model.key2);

            //    this.ctxtYw3.Text = model.ctxtYw3;

            //    SetCheckCom(this.ccomyw3, model.key3);

            //    this.ctxtYw4.Text = model.ctxtYw4;

            //    SetCheckCom(this.ccomyw4, model.key4);

            //    this.ctxtYw5.Text = model.ctxtYw5;

            //    SetCheckCom(this.ccomyw5, model.key5);

            //    this.ctxtYw6.Text = model.ctxtYw6;

            //    SetCheckCom(this.ccomyw6, model.key6);
            //}
        }

        private void gbtnAddF_Click(object sender, EventArgs e)
        {
            //  List<PosFieldDetail> ls = common.GetPosConfig_1(config_dir, posFieldDetail);

            if (pLs == null)
                pLs = new List<PosFieldDetail>();


            string fieldName = this.ctxtFieldValue.Text;
            string fieldValue = this.cCombox2.SelectedValue.ToString();
            string sort = this.ctxtSort.Text;
            string width = this.ctxtWidth.Text;

            if (sort == "" || !ValidationHelper.IsInt(sort))
            {
                MessageBox.Show("排序必须为整数");
                return;
            }
            if (width == "" || !ValidationHelper.IsInt(width))
            {
                MessageBox.Show("宽度必须为整数");
                return;
            }
            PosFieldDetail model = new PosFieldDetail();
            model.FieldName = fieldName;
            model.FieldValue = fieldValue;
            model.Sort = int.Parse(sort);
            model.Width = int.Parse(width);

            bool isUpdate = false;
            foreach (PosFieldDetail p1 in pLs)
            {
                if (p1.FieldValue == fieldValue)
                {
                    p1.FieldName = model.FieldName;
                    p1.Sort = model.Sort;
                    p1.Width = model.Width;
                    isUpdate = true;
                }

            }
            if (!isUpdate)
                pLs.Add(model);


            common.SetPosConfig(pLs, posFieldDetail);


            dataGridView2.DataSource = null;

            this.dataGridView2.DataSource = pLs;


            //var query = (from f in pLs where (f.FieldValue == fieldValue)  select f).FirstOrDefault();
            ////参数
            //if (query != null)
            //{
            //}



        }

        private void gbtnAdd_Click(object sender, EventArgs e)
        {

        }

        private void cCombox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.ctxtFieldValue.Text = this.cCombox2.Text;
        }

        private void dataGridView2_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            int rowIndex = e.RowIndex;
            if (dataGridView2.Rows[rowIndex] != null)
            {
                string name = dataGridView2.Rows[rowIndex].Cells[1].Value.ToString();
                for (int i = pLs.Count - 1; i >= 0; i--)
                {
                    if (pLs[i].FieldValue == name)
                        pLs.Remove(pLs[i]);
                }
            }
            common.SetPosConfig(pLs, posFieldDetail);

            dataGridView2.DataSource = null;
            this.dataGridView2.DataSource = pLs;
        }

        private void dataGridView2_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {

        }
    }


}
